package com.fingard.dsp.bank.directbank.weixin01.domain;

import com.fingard.dsp.bank.directbank.weixin01.domain.common.DomainConfig;
import com.fingard.dsp.bank.directbank.weixin01.domain.common.DomainConstants;
import com.fingard.dsp.bank.directbank.weixin01.domain.common.DomainUtil;

/**
 * 【跨城冗灾】对外抽象类
 * @author liujinhui
 */
abstract class AbstractDomainService {

    // 微信支付域名
    private static String API_MCH_WEIXIN_DOMAIN = "api.mch.weixin.qq.com";
    // 公众平台域名
    private static String API_WEIXIN_DOMAIN = "api.weixin.qq.com";

    private static DomainConfig config;

    protected static DomainConfig getConfig() {
        return config;
    }

    protected AbstractDomainService() {
        AbstractDomainService.config = new DomainConfig.Builder().build();
    }

    protected AbstractDomainService(DomainConfig config) {
        if (config == null) {
            throw new IllegalArgumentException("配置信息未初始化");
        }
        AbstractDomainService.config = config;
    }

    /**
     * 根据传入域名获取另一个域名，即传入主域名返回备用域名、传入备用域名返回主域名
     * @param domain 当前使用的域名
     * @return
     */
    public String getInverseDomain(String domain) {
        String inverseDomain = domain;
        if (!DomainUtil.StringIsEmpty(domain)) {
            if (DomainConstants.API_MCH_WEIXIN_DOMAIN.equals(domain)) {
                inverseDomain = DomainConstants.API_MCH_WEIXIN_RESERVE_DOMAIN;
            } else if (DomainConstants.API_MCH_WEIXIN_RESERVE_DOMAIN.equals(domain)) {
                inverseDomain = DomainConstants.API_MCH_WEIXIN_DOMAIN;
            } else if (DomainConstants.API_WEIXIN_DOMAIN.equals(domain)) {
                inverseDomain = DomainConstants.API_WEIXIN_RESERVE_DOMAIN;
            } else if (DomainConstants.API_WEIXIN_RESERVE_DOMAIN.equals(domain)) {
                inverseDomain = DomainConstants.API_WEIXIN_DOMAIN;
            } else {
                throw new IllegalArgumentException("域名错误");
            }
        }
        return inverseDomain;
    }

    /**
     * 获取双活处理后的域名
     * @return
     * @throws
     */
    public String getDomain(DomainConstants.DomainType domainType) {
        String domain = null;
        if (domainType == null) {
            throw new IllegalArgumentException("未指定域名类型");
        }
        if (domainType != null) {
            if (DomainConstants.DomainType.API_MCH.equals(domainType)) {
                domain = API_MCH_WEIXIN_DOMAIN;
            } else if (DomainConstants.DomainType.API.equals(domainType)) {
                domain = API_WEIXIN_DOMAIN;
            }
            if (domain != null) {
                domain = DomainUtil.reserveRate(domain,config.getReserveDomainReferRate());
            }
        }
        return domain;
    }

    /**
     * 上报请求结果
     * @param reportResult (0:失败,1:成功，必填)
     * @param domain (当前请求域名，必填)
     */
    public void reportResult(DomainConstants.ReportResult reportResult, String domain) {

    }

    /**
     * 修改当前域名
     * @param domain 域名
     */
    public void setDomain(DomainConstants.DomainType domainType, String domain) {
        if (DomainConstants.DomainType.API_MCH.equals(domainType)) {
            API_MCH_WEIXIN_DOMAIN = domain;
        } else if (DomainConstants.DomainType.API.equals(domainType)) {
            API_WEIXIN_DOMAIN = domain;
        }
    }
}
